﻿using System.Windows.Input;

namespace BeerManagementSystem
{
    class KeyManager
    {

        private static KeyManager _keyManager;
        private KeyManager()
        {
            CheckIn.Instance.KeyDown += KeyDownHandler;
            LogWriter.Write("KeyManager initialized.", LogWriter.LogLevel.Status, this);
        }

        public void KeyDownHandler(object sender, KeyEventArgs e)
        {
            // Global Keys
            if (e.KeyboardDevice.Modifiers != ModifierKeys.Control)
            {
                switch (e.Key)
                {
                    case Key.Enter:
                        if (Controller.Instance.InviteMode) Controller.Instance.DisableInviterMode();
                        else if (CheckIn.Instance.AssignMode()) Controller.Instance.QuestionUserExists();
                        else if (!CheckIn.Instance.BeerboxWriteMode) Controller.Instance.EnterBeerBox();
                        else
                        {
                            Controller.Instance.SendBeerboxMessage();
                        }
                        break;
                    case Key.F1:
                        Controller.Instance.ChangeDrink(Drink.Beer);
                        break;
                    case Key.F2:
                        Controller.Instance.ChangeDrink(Drink.Wine);
                        break;
                    case Key.F3:
                        Controller.Instance.ChangeDrink(Drink.Shot);
                        break;
                    case Key.Escape:
                        if (CheckIn.Instance.AssignMode()) CheckIn.Instance.FinalizeAssignCard();
                        else if (CheckIn.Instance.BeerboxIsOpen) CheckIn.Instance.BeerboxReleaseFocus();
                        break;
                    case Key.Y:
                        if(Controller.Instance.SuggestionCreateUser) Controller.Instance.CreateUser();
                        else if(Controller.Instance.SuggestionOverideAssignment) Controller.Instance.AssigningUserToCard();
                        break;
                    case Key.N:
                        if (Controller.Instance.SuggestionCreateUser)
                        {
                            Controller.Instance.SuggestionCreateUser = false;
                            CheckIn.Instance.FinalizeAssignCard();
                        }
                        else if (Controller.Instance.SuggestionOverideAssignment) CheckIn.Instance.FinalizeAssignCard();
                        break;
                }
            }
                //Ctrl Modifier + key
            else
            {
                switch (e.Key)
                {
                    case Key.D0:
                        Controller.Instance.BarrelReset();
                        break;
                    case Key.Q:
                        Controller.Instance.CloseCheckIn();
                        break;
                    case Key.W:
                        Controller.Instance.CloseCurrentTab();
                        break;
                    case Key.Left:
                        Controller.Instance.ToggleBeerDoctor("Left");
                        break;
                    case Key.Right:
                        Controller.Instance.ToggleBeerDoctor("Right");
                        break;
                    case Key.Tab:
                        CheckIn.Instance.ToggleTab("Right");
                        break;
                    case Key.D:
                        Controller.Instance.ToggleBeerDoctor();
                        break;
                    case Key.Back:
                        Controller.Instance.ResetLastTransaction();
                        break;
                    case Key.Z:
                        Controller.Instance.ResetLastTransaction();
                        break;
                    case Key.I:
                        Controller.Instance.StartInviteMode();
                        break;
                    case Key.Enter:
                        Controller.Instance.EnterBeerBox();
                        break;
                    case Key.G:
                        Controller.Instance.EnterGamesMode();
                        break;
                    case Key.L:
                        Controller.Instance.ShowLog();
                        break;
                    case Key.T:
                        Controller.Instance.TestMethod2();
                        break;
                    case Key.A:
                        Controller.Instance.EnterAssignUserCard();
                        break;
                    case Key.D1:
                        Controller.Instance.GuestCardSwipe(1);
                        break;
                    case Key.D2:
                        Controller.Instance.GuestCardSwipe(2);
                        break;
                    case Key.D3:
                        Controller.Instance.GuestCardSwipe(3);
                        break;
                    case Key.D6:
                        Controller.Instance.TestMethod2()
                        ;
                        break;
                    case Key.M:
                        Controller.Instance.GuestCardSwipe(12);
                        break;
                    case Key.B:
                        CheckIn.Instance.ToggleBeerbox();
                        break;
                    case Key.Down:
                        if( CheckIn.Instance.BeerboxIsOpen) CheckIn.Instance.BeerBoxGetFocus();
                        break;


                }
            }
        }

        public static void Init()
        {
            if(_keyManager == null)
                _keyManager = new KeyManager();
        }
    }
}
